package com.study.d014Bounds;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/** 碰撞例子
*@description
*@author xtayhame
*date 2020年3月4日
*version
*since v1.0
*/
public class CollideMain extends Application{

	public static void main(String[] args) {

		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		Button b1 = new Button("移动");
		Button b2 = new Button("b u t t o n");

		anchorPane.getChildren().addAll(b1,b2);

		AnchorPane.setTopAnchor(b2, 200.0);
		AnchorPane.setLeftAnchor(b2, 200.0);

		Scene scene = new Scene(anchorPane, 400, 500);

		stage.setScene(scene);
		stage.show();

		System.out.println( "b2的坐标："+b2.getLayoutX() +"---"+ b2.getLayoutY());

		Point2D point2=b2.localToParent(b2.getLayoutX(), b2.getLayoutY());

		scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
			int X=0;
			int Y=0;
			Point2D point2=null;
			@Override
			public void handle(KeyEvent event) {
				if(event.getCode().getName().equals(KeyCode.LEFT.getName())){
					X = X -10;
					System.out.println("向左移动");
				}
				else if(event.getCode().getName().equals(KeyCode.RIGHT.getName())){
					X = X + 10;
					System.out.println("向右移动");
				}
				else if(event.getCode().getName().equals(KeyCode.UP.getName())){
					Y = Y -10;
					System.out.println("向上移动");
				}
				else if(event.getCode().getName().equals(KeyCode.DOWN.getName())){
					Y = Y + 10;
					System.out.println("向下移动");
				}

				b1.setLayoutX(X);
				b1.setLayoutY(Y);


				if((b1.getLayoutX() + b1.getWidth()) >=b2.getLayoutX() && b1.getLayoutX() <=(b2.getLayoutX()+b2.getWidth())){
					System.out.println("----------XXX相撞了---------");
					if((b1.getLayoutY() + b1.getHeight()) >=b2.getLayoutY() && (b1.getLayoutY() <=(b2.getLayoutY()+b2.getHeight()))){
						System.out.println("----------YYY相撞了---------");
					}
				}
				System.out.println(X + "--" + Y);
			}
		});

	}


}
